package com.jhws.sys.cms.controller;

import com.baidu.ueditor.ActionEnter;
import com.jhws.business.aliyun.service.OSSClientService;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.common.base.bo.Global;
import com.jhws.sys.cms.bo.ImagesOss;
import com.jhws.sys.cms.bo.RichText;
import com.jhws.sys.cms.service.ImagesOssService;
import com.jhws.sys.cms.service.RichTextService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 富文本控制器
 */
@Controller
@RequestMapping("/cms/richText")
public class RichTextController {
    @Resource
    private RichTextService richTextService;
    @Resource
    private ImagesOssService imagesOssService;
    @Resource
    private OSSClientService ossClientService;

    /**
     * 百度富文本获取配置文件
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return 是否成功
     */
    @RequestMapping("/config.x")
    @ResponseBody
    public String config(HttpServletRequest request, HttpServletResponse response) {
        // response.setContentType("application/json");
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        response.setHeader("Content-Type", "text/html");
        String exec = new ActionEnter(request, rootPath, "pages/business/ueditor/jsp/").exec();
        System.out.println(exec);
        return exec;
    }

    /**
     * 保存富文本
     * @param ueEditor
     * @return
     */
    @RequestMapping("/saveEditor.x")
    @ResponseBody
    public JsonRespData saveEditor(RichText ueEditor) {
        richTextService.saveUeditor(ueEditor);
        imagesOssService.updateControlled(ueEditor.getId(), Global.OSS_IMAGES_CONTROLLED);
        return new JsonRespData("", 1, "成功");
    }

    /**
     * 查询所有富文本列表
     * @param param 查询条件
     * @return
     */
    @RequestMapping("queryEditorList.x")
    @ResponseBody
    public Map<String, Object> queryEditorList(@RequestParam Map param) {
        return richTextService.queryUeditorList(param);
    }

    /**
     * 删除富文本
     * @param ueEditor id
     * @return
     */
    @RequestMapping("deleteEditor.x")
    @ResponseBody
    public JsonRespData deleteEditor(RichText ueEditor) {
        richTextService.deleteUeditor(ueEditor);
        List<ImagesOss> imagesOsses = imagesOssService.findImagesOssByWhere(" where resource_uid='" + ueEditor.getId() + "'");
        try {
            for (ImagesOss imagesOss : imagesOsses) {
                ossClientService.deleteObject(imagesOss.getOssUrl());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        imagesOssService.deleteImagesOss(" where resource_uid = '" + ueEditor.getId() + "' ");
        return new JsonRespData("", 1, "");
    }

    /**
     * 查询富文本信息
     * @param ueditorId id
     * @return
     */
    @RequestMapping("queryEditorInfo.x")
    @ResponseBody
    public JsonRespData queryEditorInfo(String ueditorId) {
        RichText ueEditor = richTextService.queryUeditorInfo(ueditorId);
        return new JsonRespData("", 1, ueEditor);
    }
}
